let express = require('express')
let pathLib = require('path')
let fs = require('fs')
let bcryte = require('../../utils/bcrypt')

//1.创建路由
let router = express.Router();

//2.路由接口做出响应
router.post('/', (req, res, next) => {
    // console.log('路由做出响应,reg')
    //1.先获取用户传过来的字段,然后进行进行处理-----从前端的from表单中提取数据
    let { username, password, nikename } = req.body
    if (!username || !password) {
        res.send({ err: 1, msg: '用户名和密码为必传参数' })
        return
    };
    //整理传过来的文件
    // nikename = nikename || 第三方的自动生成字符库()
    nikename = nikename || '系统自动生成'
    let follow = 0;
    let fans = 0;
    let icon = '/upload/user/dafault.jpg'; //默认头像
    let time = Date.now();
    //接收传递过来的文件,然后进行改名
    // if (req.files && req.files.length > 0) {
    //     // let oldname = req.files[0].path
    //     // let newname = req.files[0].path + pathLis.parse(req.files[0].originalname).ext
    //     fs.renameSync(req.files[0].path, req.files[0].path + pathLib.parse(req.files[0].originalname).ext);
    //     icon = '/upload/user/' + req.files[0].filename + pathLib.parse(req.files[0].originalname).ext;
    // };


    if (req.files && req.files.length > 0) {
        //改名 给图片加后缀,覆盖默认头像
        fs.renameSync(
            req.files[0].path,
            req.files[0].path + pathLib.parse(req.files[0].originalname).ext
        )
        icon = 'public/upload/user/' + req.files[0].filename + pathLib.parse(req.files[0].originalname).ext
    };


    // console.log('11111111111111', icon);
    //存储数据
    req.mgdb.open({ collectionName: 'user' }).then(
        ({ collection, client }) => {
            collection.find({ username }).toArray((err, result) => {
                if (err) {
                    res.send({ err: 1, msg: '集合操作失败' })
                    client.close()
                } else {
                    if (result.length === 0) {
                        password = bcryte.hashSync(password);
                        //入库
                        collection.insertOne({ username, password, nikename, fans, follow, time, icon }, (err, result) => {
                            if (!err) {
                                // 返回前端的数据不能又密码和账号
                                delete result.ops[0].username
                                delete result.ops[0].password
                                res.send({ err: 0, msg: '注册成功', data: result.ops[0] });
                                // res.send({ err: 0, msg: '注册成功' })
                            } else {
                                res.send({ err: 1, msg: '注册失败咯' })
                            }
                        })
                    } else {
                        //注册失败,删掉上传的头像
                        if (icon.indexOf('default') == -1) {
                            fs.unlinkSync(icon)
                        }
                        res.send({ err: 1, msg: '用户名已经存在' })
                        client.close()
                    }
                }
            })
        }
    )


})

//3.导出路由
module.exports = router;